package com.cloudxingkong.dianyinke.Student.Fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.PingFenAdapter;
import com.cloudxingkong.dianyinke.Student.Ui.CourseBooking;
import com.cloudxingkong.dianyinke.Student.bean.PingFen;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 评分Fragment
 */
public class PingFenFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private ListView listView;
    private PingFenAdapter adapter;
    private List<PingFen.ListBean> list;
    private SharedPreferences sharedPreferences;
    private SwipeRefreshLayout swipeRefreshLayout;
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_ping_fen, null);
        init();
        loadData();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), CourseBooking.class);
                intent.putExtra("teacherID", list.get(position).getTeacherid() + "");
                intent.putExtra("tMemname", list.get(position).getMemname());
                intent.putExtra("teacherPIC",list.get(position).getCoverpic());
                startActivity(intent);
            }
        });
        return view;
    }

    private void init() {
        sharedPreferences = getActivity().getSharedPreferences("data_dyk", 0);

       listView = (ListView) view.findViewById(R.id.lv_pingfen);
        list = new ArrayList<>();
        adapter = new PingFenAdapter(list,getContext());
        listView.setAdapter(adapter);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.pf_swiperefresh);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        //设置自动刷新
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
    }

    private void loadData() {
        final String sessionid = CookieHelper.getSessionID(sharedPreferences);
        final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.FUJINLAOSHI;
        RequestParams params = new RequestParams(upLoadUrl);
        params.addHeader("cookie", sessionid);
        params.setMultipart(true);
        //根据评分avat查询老师详情
        params.addBodyParameter("order", "evat");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "评分老师 " + result);
                if (result!=null&&!result.equals("")){
                Message msg = handler.obtainMessage();
                msg.what = 1;
                msg.obj = result;
                handler.sendMessage(msg);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            handler.sendEmptyMessage(3);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
    }

    private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
               switch(msg.what ) {
                   case 1:
                    String state = (String) msg.obj;
                    if (state != null&&!state.equals("")) {
                        Gson gson = new Gson();
                        PingFen pf = null;
                        pf = gson.fromJson(state, PingFen.class);
                        list.addAll(pf.getList());
                       // Toast.makeText(getActivity(), "获取评分老师成功！", Toast.LENGTH_SHORT).show();
                        swipeRefreshLayout.setRefreshing(false);
                        adapter.notifyDataSetChanged();
                    }else {
                        Toast.makeText(getActivity(), "获取评分老师失败，请重试！", Toast.LENGTH_SHORT).show();
                    }
                       break;
                   case 3:
                       swipeRefreshLayout.setRefreshing(false);
                       Toast.makeText(getActivity(), "获取失败，请重试！", Toast.LENGTH_SHORT).show();
                       break;
               }
            }
        };

    @Override
    public void onRefresh() {
        list.clear();
        loadData();
    }
}
